

//
//  SelectView.m
//  QuickCure
//
//  Created by 王震 on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "SelectView.h"
#import <Masonry.h>

@interface SelectView ()

@property(nonatomic,strong) UIImage *currentImage;
@property(nonatomic,strong) UIButton *btn1;
@property(nonatomic,strong) UIButton *btn2;
@property(nonatomic,assign) BOOL isExpent;

@end


@implementation SelectView

-(instancetype)init{
    if (self = [super init]) {
        [self setBackgroundImage];
    }
    return self;
}

-(void)setBackgroundImage{
    
    //设置UIView 的背景图片的三种方式
    //NO.1 设置view的背景颜色  colorWithPatternImage
    //NO.2  覆盖一个UIImageView,再sendToBack
    //NO.3  为view的layer.contents赋值一张图片作为背景图片(最好先赋值背景图片,再添加其余子控件)
    
    //    UIImage *iamge = [UIImage imageNamed:@"shurukuang"];
    //
    //    UIColor *bj = [UIColor colorWithPatternImage:iamge];
    //    self.backgroundColor = bj;
    
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    //    self.layer.contents = iamge;
}



-(void)setImageWithText:(NSString *)nomalText{
    
    [self setTitleTextWith:nomalText];
    
    UIImage *image1 = [UIImage imageNamed:@"illness_button_input_blue.png"];
    UIImage *image2 = [UIImage imageNamed:@"illness_img_line.png"];
    UIImageView *imgv1 = [[UIImageView alloc] initWithImage:image1];
    UIImageView *imgv2 = [[UIImageView alloc] initWithImage:image2];
    [self addSubview:imgv1];
    [self addSubview:imgv2];
    
    [imgv1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self).offset(-10);
        make.width.equalTo(@(15));
        make.height.equalTo(@(15));
    }];
    [imgv2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-5);
        make.trailing.equalTo(imgv1.mas_leading).offset(-10);
        make.width.equalTo(@(2));
    }];
    
}

-(void)setButtonsWithNomalText:(NSString *)nomalText andText:(NSString *)text1 andText:(NSString *)text2{
    
    [self setTitleTextWith:nomalText];
    
    [self addSubview:self.btn1];
    [self addSubview:self.btn2];
    
    [self.btn1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置frame
    [self.btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self).offset(-10);
    }];
    [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.trailing.equalTo(self.btn1.mas_leading).offset(-20);
    }];
    
    //设置图片和文字
    [self.btn1 setTitle:text2 forState:UIControlStateNormal];
    [self.btn1 setTitle:text2 forState:UIControlStateDisabled];
    [self.btn1 setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [self.btn1 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateDisabled];
    [self.btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn1.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    [self.btn2 setTitle:text1 forState:UIControlStateNormal];
    [self.btn2 setTitle:text1 forState:UIControlStateDisabled];
    [self.btn2 setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [self.btn2 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateDisabled];
    [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn2.titleLabel setFont:[UIFont systemFontOfSize:16]];
}

-(void)btn1Click:(UIButton *)sender{
    
    sender.enabled = !sender.isEnabled;
    self.btn2.enabled = !sender.isEnabled;
    self.titleLabel.text = sender.titleLabel.text;
    [self.titleLabel setTextColor:[UIColor blackColor]];
    
    self.isExpent = !self.btn1.isEnabled;
    if (self.expentBlock) {
        
        self.expentBlock(self.isExpent);
        
    }
}
-(void)btn2Click:(UIButton *)sender{
    
    sender.enabled = !sender.isEnabled;
    self.btn1.enabled = !sender.isEnabled;
    self.titleLabel.text = sender.titleLabel.text;
    [self.titleLabel setTextColor:[UIColor blackColor]];
    
    self.isExpent = !self.btn1.isEnabled;
    if (self.expentBlock) {
        
        self.expentBlock(self.isExpent);
        
    }
}

-(void)setTitleTextWith:(NSString *)titleText{
    
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(self).offset(15);
        make.width.equalTo(@(300));
    }];
    [self.titleLabel setText:titleText];
    [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.titleLabel setTextColor:[UIColor grayColor]];
}

#pragma mark - 控件懒加载
-(UILabel *)titleLabel{
    if (nil == _titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}
-(UIButton *)btn1{
    
    if (nil == _btn1) {
        _btn1 = [[UIButton alloc] init];
    }
    return _btn1;
}
-(UIButton *)btn2{
    if (nil == _btn2) {
        _btn2 = [[UIButton alloc] init];
    }
    return _btn2;
}

@end
